# -*- coding: utf-8 -*
#! /usr/bin/env python
import serial
import time

ser = serial.Serial('/dev/ttyAMA0', 115200)  # 设置端口和波特率
if ser.isOpen == False:  # 检查串口是否打开
    ser.open()                # 打开串口
ser.write(b"Raspberry pi is ready")
try:
    while True:
        size = ser.inWaiting()               # 获得缓冲区字符
        if size != 0:
            response = ser.read(size)        # 读取内容并显示
            print(response)
            ser.flushInput()                 # 清空接收缓存区
            time.sleep(0.5)                  # 软件延时
except KeyboardInterrupt:
    ser.close()
